package com.ohos.connect.devicecloud.iot.modular.device.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connect.devicecloud.iot.model.dto.*;
import com.ohos.connect.devicecloud.iot.model.dto.deviceactive.DeviceActiveDTO;
import com.ohos.connect.devicecloud.iot.model.dto.deviceinfosync.DeviceInfoSyncDTO;
import com.ohos.connect.devicecloud.iot.model.vo.DeviceAuthCodeVO;
import com.ohos.connect.devicecloud.iot.model.vo.deviceactive.DeviceActiveVO;
import com.ohos.connect.devicecloud.iot.model.vo.DeviceLoginVO;
import com.ohos.connect.devicecloud.iot.model.vo.DevicePskVO;
import com.ohos.connect.devicecloud.iot.modular.device.model.dto.*;
import com.ohos.connect.devicecloud.iot.modular.device.model.vo.DeviceRegisterCodeVO;
import com.ohos.connect.devicecloud.iot.modular.device.model.vo.DeviceRegisterInfoVO;
import com.ohos.connect.devicecloud.iot.modular.device.model.vo.shadowdata.DeviceShadowDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ohos.connect.devicecloud.common.annotation.CommonLog;
import com.ohos.connect.devicecloud.common.pojo.CommonResult;
import com.ohos.connect.devicecloud.common.pojo.CommonValidList;
import com.ohos.connect.devicecloud.iot.modular.device.model.entity.Device;
import com.ohos.connect.devicecloud.iot.modular.device.model.param.DeviceAddParam;
import com.ohos.connect.devicecloud.iot.modular.device.model.param.DeviceEditParam;
import com.ohos.connect.devicecloud.iot.modular.device.model.param.DeviceIdParam;
import com.ohos.connect.devicecloud.iot.modular.device.service.DeviceService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 设备控制器
 * @author Dongtao
 * @date  2024/12/17 10:43
 */
@Api(tags = "设备控制器")
@ApiSupport(author = "OHOS_CONNECT_TEAM", order = 1)
@RestController
@RequestMapping(value = "/api/v1/device_cloud/iot/device")
@Validated
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取设备注册认证码")
    @CommonLog("获取设备注册认证码")
    @PostMapping("/registryCode")
    public CommonResult<DeviceRegisterCodeVO> getAuthCode(@RequestBody @Valid DeviceRegisterCodeDTO deviceRegisterCodeDTO) {
        return CommonResult.data(deviceService.getRegisterAuthCode(deviceRegisterCodeDTO));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取设备激活状态")
    @CommonLog("获取设备激活状态")
    @GetMapping("/{projectId}/{devId}/activeStatus")
    public CommonResult<Boolean> getDeviceActiveStatus(@PathVariable("projectId") String projectId, @PathVariable("devId") String devId ) {
        return CommonResult.data(deviceService.getDeviceActiveStatus(projectId,devId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "设备代理注册")
    @CommonLog("设备代理注册")
    @PostMapping("/{projectId}/device-register")
    public CommonResult<DeviceRegisterInfoVO> deviceRegister(@RequestBody @Valid DeviceRegisterInfoInsertDTO deviceRegisterInfoInsertDTO ) {
        return CommonResult.data(deviceService.appRegisterDevice(deviceRegisterInfoInsertDTO));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改设备名称")
    @CommonLog("修改设备名称")
    @PostMapping("/{projectId}/｛devId｝")
    public CommonResult<Boolean> changeDeviceName(@PathVariable("projectId") String projectId, @PathVariable("devId") String devId , @RequestBody @Valid DeviceNameUpdateDTO deviceNameUpdateDTO ) {
        return CommonResult.data(deviceService.changeDeviceName(projectId, devId, deviceNameUpdateDTO));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "控制设备")
    @CommonLog("控制设备")
    @PostMapping("/command")
    public CommonResult<Boolean> controlDevice(@RequestBody @Valid DeviceControlDTO deviceControlDTO) {
        return CommonResult.data(deviceService.deviceControl(deviceControlDTO));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取设备本地控制码")
    @CommonLog("获取设备本地控制码")
    @PostMapping("/authcode")
    public CommonResult<List<DeviceAuthCodeVO>> getDeviceAuthCode(@RequestBody @Valid List<DeviceAuthCodeDTO> deviceAuthCodeDTOList ) {
        return CommonResult.data(deviceService.getDeviceAuthCodeList(deviceAuthCodeDTOList));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取指定设备快照数据")
    @CommonLog("获取指定设备快照数据")
    @PostMapping("/shadow")
    public CommonResult<DeviceShadowDataVO> getDeviceAuthCode(@RequestBody @Valid DeviceShadeDataGetDTO deviceShadeDataGetDTO) {
        return CommonResult.data(deviceService.getDeviceShadowData(deviceShadeDataGetDTO));
    }




    // ===========================================================> 暂时不需要使用的接口


    /**
     * 添加设备
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation(value = "添加设备表",hidden = true)
    @CommonLog("添加设备表")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid DeviceAddParam deviceAddParam) {
        deviceService.add(deviceAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑设备
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation(value = "编辑设备表",hidden = true)
    @CommonLog("编辑设备表")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid DeviceEditParam deviceEditParam) {
        deviceService.edit(deviceEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除设备
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation(value = "删除设备表",hidden = true)
    @CommonLog("删除设备表")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<DeviceIdParam> deviceIdParamList) {
        deviceService.delete(deviceIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取设备详情
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation(value = "获取设备表详情",hidden = true)
    @GetMapping("/detail")
    public CommonResult<Device> detail(@Valid DeviceIdParam deviceIdParam) {
        return CommonResult.data(deviceService.detail(deviceIdParam));
    }



    // ===========================================================> 以下为展示用接口，须走coap通路实现功能

    /**
     * 交换设备sn
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation("交换设备sn--请走coap通路，本接口仅做展示用")
    @CommonLog("交换设备sn")
    @PostMapping("/exchangeDeviceSn")
    public CommonResult<DevicePskVO> exchangeDeviceSn(@RequestBody @Valid DevicePskDTO devicePskDTO) {
        return CommonResult.ok();
    }

    /**
     * 设备激活
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation("设备激活--请走coap通路，本接口仅做展示用")
    @CommonLog("设备激活")
    @PostMapping("/activeDevice")
    public CommonResult<DeviceActiveVO> activeDevice(@RequestBody @Valid DeviceActiveDTO deviceActiveDTO) {
        return CommonResult.ok();
    }

    /**
     * 设备激活
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation("设备登录--请走coap通路，本接口仅做展示用")
    @CommonLog("设备登录")
    @PostMapping("/loginDevice")
    public CommonResult<DeviceLoginVO> loginDevice(@RequestBody @Valid DeviceLoginDTO deviceActiveDTO) {
        return CommonResult.ok();
    }

    /**
     * 设备信息同步
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation("设备信息同步--请走coap通路，本接口仅做展示用")
    @CommonLog("设备信息同步")
    @PostMapping("/syncDeviceInfo")
    public CommonResult<Boolean> syncDeviceInfo(@RequestBody @Valid DeviceInfoSyncDTO deviceInfoSyncDTO) {
        return CommonResult.ok();
    }

    /**
     * 设备信息同步
     * @author Dongtao
     * @date  2024/12/17 10:43
     */
    @ApiOperationSupport(order = 99)
    @ApiOperation("设备注销--请走coap通路，本接口仅做展示用")
    @CommonLog("设备注销")
    @PostMapping("/deviceRevoke")
    public CommonResult<Boolean> deviceRevoke(@RequestBody @Valid DeviceRevokeDTO deviceRevokeDTO) {
        return CommonResult.ok();
    }




}
